import { RouteRecordRaw } from 'vue-router'
import Home from '../Layout.vue'
import Login from '../views/Login.vue'
import { NavItem } from '@/ts/model/NavItem'
import routerDataSource from './router-data-source'
const routeMenuDataList = routerDataSource

// console.log("routeMenuDataList: ", routeMenuDataList);
/***
 * 1、转成 menu 需要的数据。
 * 2、转成 breadcut 需要的数据。
 * 3、转成 routes 需要的数据。
 * 4、生成 目录对应的 vue 文件。
 * 5、生成 目录对应的 权限 sql。
 */
export function getMenuDataList(): Array<NavItem> {
  return routeMenuDataList.map((item) => {
    let children = null
    if (item.children) {
      children = item.children.map((child) => {
        return new NavItem(
          child.id,
          child.title,
          child.icon,
          child.path,
          child.perm,
          null
        )
      })
    }

    return new NavItem(
      item.id,
      item.title,
      item.icon,
      item.path,
      item.perm,
      children
    )
  })
}

export function getBreadcutDataList() {
  let indexBreadCrumbItem = {
    id: 1,
    name: '首页',
    path: '/system/index',
  }

  let breadcrumbMap: any = {}
  routeMenuDataList.forEach((item) => {
    if (item.path == '/' || item.path == '/system/index') {
      breadcrumbMap['/system/index'] = [
        {
          id: 1,
          name: '首页 /',
          path: '/system/index',
        },
      ]
      return
    }

    let children = item.children || []
    if (children.length) {
      for (let childItem of children) {
        let list = [
          indexBreadCrumbItem,
          {
            id: item.id,
            name: item.title,
            path: item.path,
          },
        ]
        list.push({
          id: childItem.id,
          name: childItem.title,
          path: childItem.path,
        })
        breadcrumbMap[childItem.path] = list
      }
    }
  })

  return breadcrumbMap
}

export function getRoutesDataList() {
  let homeRouter = {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/indexs/index',
    children: [],
  }

  console.log('homeRouter: ', routeMenuDataList)
  let dataList = routeMenuDataList as any
  let homeRouterChildren = []
  for (let item of dataList) {
    if (!item.children) {
      item.path = item.path.replace(/\.vue/, '')
      homeRouterChildren.push({
        path: item.path,
        name: item.name,
        component: item.component,
      })
      continue
    }
    let children = item.children || []
    for (let child of children) {
      child.path = child.path.replace(/\.vue/, '')
      homeRouterChildren.push({
        path: child.path,
        name: child.name,
        component: child.component,
      })
    }
  }
  homeRouter.children = homeRouterChildren as any
  return [
    homeRouter,
    {
      path: '/login',
      name: 'Login',
      component: Login,
    },
  ]
}

// const routes: Array<RouteRecordRaw> = [
//   {
//     path: "/",
//     name: "Home",
//     component: Home,
//     redirect: "/system/index",
//     children: [
//       //系统管理模块
//       {
//         path: "/system/index",
//         name: "Index",
//         component: () => import("@/views/system-setting/index.vue")
//       },
//       {
//         path: "/system/version",
//         name: "Version",
//         component: () => import("@/views/system-setting/VersionManage.vue")
//       },

//       //用户管理模块
//       {
//         path: "/users/permission",
//         name: "Permission",
//         component: () => import("@/views/user-center/PermissionManage.vue")
//       },
//       {
//         path: "/users/role",
//         name: "Role",
//         component: () => import("@/views/user-center/RoleManage.vue")
//       },
//       {
//         path: "/users/user",
//         name: "User",
//         component: () => import("@/views/user-center/UserManage.vue")
//       },

//       //设备管理
//       {
//         path: "/devices/deviceList",
//         name: "DeviceList",
//         component: () => import("@/views/device-manage/DeviceList.vue")
//       },

//       //订单管理
//       {
//         path: "/orders/orderList",
//         name: "OrderList",
//         component: () => import("@/views/order-manage/OrderList.vue")
//       },
//       {
//         path: "/orders/orderDetailList",
//         name: "orderDetailList",
//         component: () => import("@/views/order-manage/OrderDetailList.vue")
//       },

//       //短信管理
//       {
//         path: "/messages/messageList",
//         name: "MessageList",
//         component: () => import("@/views/message-manage/MessageList.vue")
//       },
//       {
//         path: "/messages/voiceList",
//         name: "VoiceList",
//         component: () => import("@/views/message-manage/VoiceMessageList.vue")
//       },
//       {
//         path: "/system/phoneMessage",
//         name: "NotFound",
//         component: () => import("@/NotFound.vue")
//       },

//       //用户反馈
//       {
//         path: "/system/feedback",
//         name: "FeedbackManage",
//         component: () => import("@/views/system-setting/FeedbackManage.vue")
//       },

//       //版本管理
//       {
//         path: "/system/version",
//         name: "VersionManage",
//         component: () => import("@/views/system-setting/VersionManage.vue")
//       }

//       //暂无功能
//     ]
//   },
//   {
//     path: "/login",
//     name: "Login",
//     component: Login
//   }
// ];

// const routes =

// export { getRoutesDataList };
